import { ref } from "vue"
export const useBufferAnalysis = (map) => {
  let draw = ref(null)
  const source = new ol.source.Vector({})
  const layer = new ol.layer.Vector({
    source,
    style: new ol.style.Style({
      //填充色
      fill: new ol.style.Fill({
        color: "rgba(255, 204, 51, 0.5)",
      }),
      //边线样式
      stroke: new ol.style.Stroke({
        color: "rgba(255,204, 51, 1)",
        width: 1,
      }),
    }),
  })
  //设置接收居民区的列表
  const areaList = ref([])
  //控制dialogue的显隐
  const bufferFlag = ref(false)
  const bufferAnalysis = () => {
    setTimeout(() => {
      map.value.addLayer(layer)
    }, 10)
    //激活画笔
    draw.value = createDraw({
      type: "LineString",
      source,
      callback: handleDraw,
    })
    map.value.addInteraction(draw.value)
  }

  function handleDraw(result) {
    /**
     * 发布公告绘制线结束的回调函数
     * @param {ol.interaction.Draw.Event} result 事件对象
     */
    var pointObj = new Array()
    for (
      var i = 0;
      i < result.feature.getGeometry().getCoordinates().length;
      i++
    ) {
      var pointGeo = new Zondy.Object.Point2D(
        result.feature.getGeometry().getCoordinates()[i][0],
        result.feature.getGeometry().getCoordinates()[i][1]
      )
      pointObj.push(pointGeo)
    }
    var gArc = new Zondy.Object.Arc(pointObj)
    //构成线的折线
    var gAnyLine = new Zondy.Object.AnyLine([gArc])
    //设置线要素的几何信息
    var gline = new Zondy.Object.GLine(gAnyLine)
    //设置要素的几何信息
    var fGeom = new Zondy.Object.FeatureGeometry({ LinGeom: [gline] })
    //设置属性结构
    var regAttStr = new Zondy.Object.CAttStruct({
      FldName: ["ID", "道路名称", "车流量", "行政区"],
      FldNumber: 4,
      FldType: ["string", "string", "int", "string"],
    })
    //实例化CAttDataRow类
    var values = [0, 62.566714, 50.803211, 0]
    var valuesRow = new Zondy.Object.CAttDataRow(values, 1)
    //实例化FeatureBuffBySingleRing类，设置要素缓冲分析必要参数，输出分析结果到缓冲分析结果图层
    var featureBufBySR = new Zondy.Service.FeatureBuffBySingleRing({
      ip: "localhost",
      port: 6163,
      //设置要素缓冲分析左半径
      leftRad: 0.002,
      //设置要素缓冲分析右半径
      rightRad: 0.002,
    })
    /*设置缓冲分析参数*/
    //设置几何信息
    featureBufBySR.sfGeometryXML = JSON.stringify([fGeom])
    //设置属性结构
    featureBufBySR.attStrctXML = JSON.stringify(regAttStr)
    //设置属性值
    featureBufBySR.attRowsXML = JSON.stringify([valuesRow])
    //设置追踪半径
    featureBufBySR.traceRadius = 0.0001
    //设置缓冲结果的名称以及存放地址
    featureBufBySR.resultName =
      "gdbp://MapGisLocal/wuhan/sfcls/buffer" + Date.now()
    //调用Zondy.Service.AnalysisBase基类的execute方法执行要素缓冲分析，AnalysisSuccess为回调函数。
    featureBufBySR.execute(bufferSuccess, "post", false, "json")
  }
  //缓冲区回调函数
  /**
   * 缓冲区分析成功回调函数，执行裁剪分析
   * @param {object} result 缓冲区分析结果对象
   */
  let clipName = null
  var clipParam = null
  let resultLayer = ref(null)
  function bufferSuccess(result) {
    var clspath = result.results[0].Value
    //实例化ClipByLayer类
    clipName = "gdbp://MapGisLocal/wuhan/sfcls/clip" + Date.now()
    clipParam = new Zondy.Service.ClipByLayer({
      ip: "localhost",
      port: 6163,
      //源简单要素类的URL
      srcInfo1: "gdbp://MapGisLocal/wuhan/sfcls/居民区",
      //裁剪框简单要素类的URL
      srcInfo2: clspath,
      //设置结果URL
      desInfo: clipName,
      infoOptType: 0,
    })
    //调用基类的execute方法，执行图层裁剪分析。AnalysisSuccess为结果回调函数
    clipParam.execute(clipSuccess, "post", false, "json")
  }
  function clipSuccess(data) {
    bufferFlag.value = true
    resultLayer.value = new Zondy.Map.GdbpLayer("", [clipName], {
      ip: "localhost",
      port: 6163,
    })
    map.value.addLayer(resultLayer.value)
    queryByAttribute(clipName, callback)
  }
  //此处是查询的回调,获取居民区列表
  function callback(data) {
    if (areaList.value) {
      areaList.value = []
    }
    data.SFEleArray.forEach((item) => {
      areaList.value.push({
        name: item.AttValue[4],
        tel: item.AttValue[5],
      })
    })
  }
  function queryByAttribute(gdbName, querySuccess) {
    //初始化查询结构对象，设置查询结构包含几何信息
    var queryStruct = new Zondy.Service.QueryFeatureStruct()
    //是否包含几何图形信息
    queryStruct.IncludeGeometry = true
    //是否包含属性信息
    queryStruct.IncludeAttribute = true
    //是否包含图形显示参数
    queryStruct.IncludeWebGraphic = false
    //实例化查询参数对象
    var queryParam = new Zondy.Service.QueryByLayerParameter(gdbName, {
      resultFormat: "json",
      struct: queryStruct,
    })
    //设置查询分页号
    queryParam.pageIndex = 0
    //设置查询要素数目
    queryParam.recordNumber = 20
    //设置属性条件
    var mapLayer = 0
    queryParam.where = mapLayer
    //queryParam.where = "name='中国'";
    //实例化地图文档查询服务对象
    var queryService = new Zondy.Service.QueryLayerFeature(queryParam, {
      ip: "localhost",
      port: "6163", //访问IGServer的端口号，.net版为6163，Java版为8089
    })
    //执行查询操作，querySuccess为查询回调函数
    queryService.query(querySuccess)
  }
  //关闭画笔,关闭对话框
  function closeBuffer() {
    source.clear()
    map.value.removeInteraction(draw.value)
    bufferFlag.value = false
    map.value.removeLayer(resultLayer.value)
  }
  return { bufferAnalysis, areaList, bufferFlag, closeBuffer }
}
